package com.vader.boot.bean;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vader
 * @date 2022/5/12
 **/
@Slf4j
public class TestMethodTemplate {

    public static void main(String[] args) {
        MyBeanFactory myBeanFactory = new MyBeanFactory();
        myBeanFactory.addBeanPostProcessor(bean -> log.info("test1"));
        myBeanFactory.addBeanPostProcessor(bean -> log.info("test2"));
        myBeanFactory.addBeanPostProcessor(bean -> log.info("test3"));
        myBeanFactory.addBeanPostProcessor(bean -> log.info("test4"));
        myBeanFactory.getBean();
    }

    static class MyBeanFactory {
        public Object getBean() {
            Object o = new Object();
            log.info("construct" + o);
            log.info("dependency_import" + o);
            for (BeanPostProcessor processor : processors) {
                processor.inject(o);
            }
            log.info("init" + o);
            return o;
        }


        private final List<BeanPostProcessor> processors = new ArrayList<>();

        public void addBeanPostProcessor(BeanPostProcessor processor){
            processors.add(processor);
        }
    }

    public interface BeanPostProcessor {
        void inject(Object bean);
    }
}
